﻿Imports Models.Security


Namespace Security


    Public Class Process

        Private groupList As Generic.List(Of ProcessGroupVO)
        Private processList As Generic.List(Of ProcessVO)


        Sub New()

            groupList = New Generic.List(Of ProcessGroupVO)
            processList = New Generic.List(Of ProcessVO)
            Dim oGroup As ProcessGroupVO


            '' SECURITY
            oGroup = New ProcessGroupVO("SECURITY", "Security")
            groupList.Add(oGroup)
            processList.Add(New ProcessVO(oGroup, "GETALLSESSIONS", "Get all sessions"))
            processList.Add(New ProcessVO(oGroup, "GETMYSESSIONS", "Get my sessions"))


            '' MODEL MANAGER
            oGroup = New ProcessGroupVO("FILEMANAGER", "File Manager")
            groupList.Add(oGroup)
            processList.Add(New ProcessVO(oGroup, "GETFILES", "Get files"))

            '' PREFERENCES
            oGroup = New ProcessGroupVO("PREFERENCE", "Preference")
            groupList.Add(oGroup)
            processList.Add(New ProcessVO(oGroup, "GETPREFERENCE", "Get Preference"))
            processList.Add(New ProcessVO(oGroup, "CREATEPREFERENCE", "Create Preference"))
            processList.Add(New ProcessVO(oGroup, "LISTPREFERENCE", "List Preferences"))
            processList.Add(New ProcessVO(oGroup, "UPDATEPREFERENCE", "Update Preference"))
            processList.Add(New ProcessVO(oGroup, "DELETEPREFERENCE", "Delete Preference"))

            '' DASHBOARDS
            oGroup = New ProcessGroupVO("DASHBOARDS", "Dashboards")
            groupList.Add(oGroup)
            processList.Add(New ProcessVO(oGroup, "GETDASHBOARD", "View dashboard"))
            processList.Add(New ProcessVO(oGroup, "CREATEDASHBOARD", "Create new dashboard"))
            processList.Add(New ProcessVO(oGroup, "DELETEDASHBOARDS", "Delete all dashboards"))
            processList.Add(New ProcessVO(oGroup, "CHANGENAMEDASHBOARD", "Change all dashboards name"))
            processList.Add(New ProcessVO(oGroup, "ORGANIZEUSERDASHBOARD", "Organize dashboard item/group"))

            ''ABM USER
            oGroup = New ProcessGroupVO("ABMUSER", "Alta Baja Modificacion de Usuarios")
            groupList.Add(oGroup)
            processList.Add(New ProcessVO(oGroup, "GETUSER", "Get user"))
            processList.Add(New ProcessVO(oGroup, "CREATEUSER", "Create new user"))
            processList.Add(New ProcessVO(oGroup, "LISTUSERS", "List user"))
            processList.Add(New ProcessVO(oGroup, "UPDATEUSER", "Update user"))
            processList.Add(New ProcessVO(oGroup, "DELETEUSER", "Delete user"))

            ''ABM COMPANY
            oGroup = New ProcessGroupVO("ABMCOMPANY", "Alta Baja Modificacion de Compañias")
            groupList.Add(oGroup)
            processList.Add(New ProcessVO(oGroup, "GETCOMPANY", "Get company"))
            processList.Add(New ProcessVO(oGroup, "CREATECOMPANY", "Create new company"))
            processList.Add(New ProcessVO(oGroup, "LISTCOMPANIES", "List companies"))
            processList.Add(New ProcessVO(oGroup, "UPDATECOMPANY", "Update company"))
            processList.Add(New ProcessVO(oGroup, "DELETECOMPANY", "Delete company"))

            ''ABM GROUP
            oGroup = New ProcessGroupVO("ABMGROUP", "Alta Baja Modificacion de Grupos")
            groupList.Add(oGroup)
            processList.Add(New ProcessVO(oGroup, "GETGROUP", "Get group"))
            processList.Add(New ProcessVO(oGroup, "CREATEGROUP", "Create new group"))
            processList.Add(New ProcessVO(oGroup, "LISTGROUPS", "List groups"))
            processList.Add(New ProcessVO(oGroup, "UPDATEGROUP", "Update group"))
            processList.Add(New ProcessVO(oGroup, "DELETEGROUP", "Delete group"))

            ''ABM ROL
            oGroup = New ProcessGroupVO("ABMROL", "Alta Baja Modificacion de Roles")
            groupList.Add(oGroup)
            processList.Add(New ProcessVO(oGroup, "GETROL", "Get rol"))
            processList.Add(New ProcessVO(oGroup, "CREATEROL", "Create new rol"))
            processList.Add(New ProcessVO(oGroup, "LISTROLS", "List rols"))
            processList.Add(New ProcessVO(oGroup, "UPDATEROL", "Update rol"))
            processList.Add(New ProcessVO(oGroup, "DELETEROL", "Delete rol"))

            ''MapGeoData VISUALS
            oGroup = New ProcessGroupVO("MAPGEODATAVISUALS", "MapGeoData Visuals")
            groupList.Add(oGroup)
            processList.Add(New ProcessVO(oGroup, "LASTMODELS", "Last Models"))
            processList.Add(New ProcessVO(oGroup, "LASTDASHBOARDS", "Last Dashboards"))
            processList.Add(New ProcessVO(oGroup, "TILESAREA", "Tiles Area"))
            processList.Add(New ProcessVO(oGroup, "MYREPORTS", "My Reports"))

        End Sub

        ''' <summary>
        ''' Sincronize process and groups 
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub sincronize()

            Dim allProcess As New Generic.List(Of String)
            Dim allGroups As New Generic.List(Of String)

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                '' add/update all process groups
                Dim dbGroupList As Generic.List(Of Data.BaseModel.ProcessGroup) = (From g In oDB.ProcessGroups Select g).ToList
                Dim found As Boolean
                For Each group As ProcessGroupVO In groupList
                    allGroups.Add(group.code)
                    found = False
                    '' locate group in db
                    For Each dbGroup As Data.BaseModel.ProcessGroup In dbGroupList
                        If dbGroup.Code = group.code Then
                            'update name
                            dbGroup.Name = group.groupName
                            found = True
                        End If
                    Next
                    '' create group if not found
                    If Not found Then
                        Dim entity As New Data.BaseModel.ProcessGroup()
                        entity.Code = group.code
                        entity.Name = group.groupName
                        oDB.ProcessGroups.AddObject(entity)
                    End If
                Next


                '' add/update all process 
                Dim dbProcessList As Generic.List(Of Data.BaseModel.Process) = (From p In oDB.Processes Select p).ToList
                For Each process As ProcessVO In processList
                    allProcess.Add(process.code)
                    found = False
                    '' locate process in db
                    For Each dbProcess As Data.BaseModel.Process In dbProcessList
                        If dbProcess.Code = process.code Then
                            'update name and group
                            dbProcess.Name = process.processName
                            If dbProcess.GroupCode <> process.group.code Then
                                dbProcess.GroupCode = process.group.code
                            End If
                            found = True
                        End If
                    Next
                    '' create group if not found
                    If Not found Then
                        Dim entity As New Data.BaseModel.Process()
                        entity.Code = process.code
                        entity.Name = process.processName
                        entity.GroupCode = process.group.code
                        oDB.Processes.AddObject(entity)
                    End If
                Next


                ' Delete process if no exists
                For Each dbProcess As Data.BaseModel.Process In dbProcessList
                    If Not allProcess.Contains(dbProcess.Code) Then
                        ''find and delete process in rols
                        Dim rols As Generic.List(Of Data.BaseModel.Rol) = (From r In oDB.Rols From p In r.Processes Where p.Code = dbProcess.Code Select r).ToList
                        For Each rol As Data.BaseModel.Rol In rols
                            rol.Processes.Remove(dbProcess)
                        Next

                        ''delete Process
                        oDB.DeleteObject(dbProcess)
                    End If
                Next

                'Delte process group if not exists
                For Each dbGroup As Data.BaseModel.ProcessGroup In dbGroupList
                    If Not allGroups.Contains(dbGroup.Code) Then
                        'delete group
                        'oDB.DeleteObject(dbGroup)
                    End If
                Next

                oDB.SaveChanges()

            End Using

        End Sub

    End Class


End Namespace